package org.jlr.packagecache;

import junit.framework.TestCase;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PackageTester extends TestCase {

    @Override
    protected void setUp() throws Exception {
    }

    public void testPublicCache() throws Exception {
        File cachedir = new File("cachedir", "packages");
        TestGlobal.packageManager = new TestPackageManager(cachedir, true);
        try {
            TestGlobal.getSearchPath().add(new TestArchive("demo.jar"));
        } catch (IOException io) {
            throw new RuntimeException("test failed");
        }
        JavaPackage j = (JavaPackage)TestGlobal.packageManager.lookupName("org.foo");
        List names = j.dir();
        Collections.sort(names);
        assertEquals(names.toString(), "[Bar, Baz]");
        assertEquals(names.size(), 2);
        Class c = (Class)TestGlobal.packageManager.lookupName("org.foo.Bar");
        Object o = c.newInstance();
        assertEquals(o.getClass().getName(), "org.foo.Bar");
    }

    public void testPrivateCache() throws Exception {
        File cachedir = new File("cachedir", "packages");
        TestGlobal.packageManager = new TestPackageManager(cachedir, false);
        try {
            TestGlobal.getSearchPath().add(new TestArchive("demo.jar"));
        } catch (IOException io) {
            throw new RuntimeException("test failed");
        }
        JavaPackage j = (JavaPackage)TestGlobal.packageManager.lookupName("org.foo");
        List names = j.dir();
        Collections.sort(names);
        assertEquals(names.toString(), "[Another, Bar, Baz, PackagePrivateOne]");
        assertEquals(names.size(), 4);
        Class c = (Class)TestGlobal.packageManager.lookupName("org.foo.Bar");
        Object o = c.newInstance();
        assertEquals(o.getClass().getName(), "org.foo.Bar");
    }
}
